#include<stdio.h>
#include<unistd.h>
#include<assert.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>



// 在程序替换的时候替换自己的程序
int main()
{
    printf("process is running\n");
    pid_t id = fork();
    assert(id != -1);
    
    if(id == 0)
    {
        // child 
        // 让子进程去完成进程程序替换
        sleep(1); // 为了让这个现象更加明显一些
        execl("./mybin", "./mybin", NULL);
        exit(1);
    }

    // parent
    int status = 0;
    pid_t ret = waitpid(id, &status, 0);
    if(ret > 0)
    {
        // 成功回收子进程
        printf("wait success: %d, sign number: %d, exit code: %d\n", ret, status & 0x7F, (status >> 8) & 0xFF);
    }

    return 0;
}
